package org.algorithm.Stack;

import java.lang.reflect.Array;

public class ArrayStack<E> implements Stack<E>{

    private E[] array;
    private int top;//栈顶指针


    /**
     * 栈底在数组的头部
     * 栈顶在数组的尾部
     * @param capacity
     */

    public ArrayStack(int capacity)
    {
        this.array= (E[]) new Object[capacity];
    }

    @Override
    public boolean push(E value) {
        if (isFull()) {
            return false;
        }

        array[top]=value;
        top++;
        return true;
    }

    @Override
    public E pop() {
        if (isEmpty()) {
            return null;
        }

        E value=array[top-1];
        top--;
        return value;
    }

    @Override
    public E peek() {
        if (isEmpty()) {
            return null;
        }

        return array[top-1];
    }

    @Override
    public boolean isEmpty() {
        return top==0;
    }

    @Override
    public boolean isFull() {
        return top==array.length;
    }
}
